let systemInfo;
export const getSystemInfo = () => {
    if (systemInfo)
        return systemInfo;
    const info = uni.getSystemInfoSync();
    let isMac = info.platform.toLowerCase().includes('mac');
    const isIos = info.system ? info.system.toLowerCase().includes('ios') : isMac;
    let isWeixin = true;
    let isAlipay = false;
    let isApp = false;
    let isMp = false;
    let isMpWeixin = false;
    let isMpAlipay = false;
    // #ifdef MP-ALIPAY
    isWeixin = false;
    isAlipay = true;
    isMpAlipay = true;
    // #endif
    // #ifdef MP-WEIXIN
    isMpWeixin = true;
    // #endif
    // #ifdef MP
    isMp = true;
    // #endif
    // #ifdef APP-PLUS
    isApp = true;
    // #endif
    // #ifdef H5
    const ua = navigator.userAgent;
    isMac = /macintosh|mac os x/i.test(ua);
    isWeixin = /micromessenger/.test(ua.toLowerCase());
    isAlipay = /alipay/.test(ua.toLowerCase());
    // #endif
    // 小程序胶囊按钮信息
    let mpMenuButtonRect = uni.getMenuButtonBoundingClientRect &&
        uni.getMenuButtonBoundingClientRect();
    // 有些值可能为null或0
    if (!mpMenuButtonRect ||
        !mpMenuButtonRect.left ||
        !mpMenuButtonRect.top ||
        !mpMenuButtonRect.width ||
        !mpMenuButtonRect.height) {
        let gap = 0; // 胶囊在TitleBar中上下间距，使导航垂直居中
        let width = 96; // 胶囊宽度
        if (info.platform === 'devtools') {
            gap = isIos ? 5.5 : 7.5;
        }
        else {
            gap = isIos ? 4 : 8;
            width = isIos ? 88 : 96;
        }
        if (!info.statusBarHeight) {
            // 开启wifi的情况下修复statusBarHeight值获取不到
            info.statusBarHeight = info.screenHeight - info.windowHeight - 20;
        }
        mpMenuButtonRect = {
            width,
            height: 32,
            bottom: info.statusBarHeight + gap + 32,
            left: info.windowWidth - width - 10,
            right: info.windowWidth - 10,
            top: info.statusBarHeight + gap,
        };
    }
    let navBarHeight = 44;
    if (!info.statusBarHeight) {
        navBarHeight =
            (mpMenuButtonRect.top - (info.screenHeight - info.windowHeight - 20)) *
                2 +
                mpMenuButtonRect.height;
        info.statusBarHeight = 0;
    }
    else {
        navBarHeight =
            info.statusBarHeight +
                (mpMenuButtonRect.top - info.statusBarHeight) * 2 +
                mpMenuButtonRect.height;
    }
    return {
        devicePixelRatio: info.pixelRatio,
        language: info.language || '',
        windowWidth: info.windowWidth,
        windowHeight: info.windowHeight,
        statusBarHeight: info.statusBarHeight,
        navBarHeight,
        mpMenuButtonRect,
        isIos,
        isMac,
        isWeixin,
        isAlipay,
        isApp,
        isMp,
        isMpWeixin,
        isMpAlipay,
    };
};
/**
 * 判断是否是微信小程序Web环境
 */
export const isMpWeixinWeb = () => false;
/**
 * 当前运行环境是否是在开发组件库
 */
export const isDevPackage = () => false;
/**
 * 判断是否是微信小程序插件Web环境
 */
export const getWxMpPluginEnv = () => '';
/**
 * 获取输入内容
 * @param event 输入事件
 */
export function getInputValue(event) {
    return event.detail.value;
}
const resolveSaveImgAuth = () => {
    return new Promise((resolve, reject) => {
        let scope = 'scope.writePhotosAlbum';
        // #ifdef MP-TOUTIAO
        scope = 'scope.album';
        // #endif
        // #ifdef MP
        // 相册权限
        uni.authorize({
            scope,
            success: resolve,
            fail(e) {
                uni.hideLoading();
                uni.showModal({
                    title: '温馨提示',
                    content: '请开启相册权限后再操作' + JSON.stringify(e),
                    confirmText: '去开启',
                    success: function (res) {
                        if (res.confirm) {
                            uni.openSetting({});
                        }
                    },
                });
                reject();
            },
        });
        // #endif
        // #ifndef MP
        resolve(1);
        // #endif
    });
};
/**
 * 保存图片
 * @param url
 */
export function saveImage(url) {
    if (!url)
        return;
    uni.showLoading({ title: '正在保存...' });
    resolveSaveImgAuth().then(() => {
        uni.getImageInfo({
            src: url,
            success(ret) {
                // 保存
                uni.saveImageToPhotosAlbum({
                    filePath: ret.path,
                    success() {
                        uni.hideLoading();
                        uni.showToast({ title: '已保存到相册' });
                    },
                    fail(err) {
                        uni.hideLoading();
                        uni.showToast({
                            title: '保存失败,请稍后重试...' + err.errMsg,
                            icon: 'none',
                            duration: 2200,
                        });
                    },
                });
            },
            fail(err) {
                uni.hideLoading();
                uni.showToast({
                    title: '保存失败,请稍后重试...' + err.errMsg,
                    icon: 'none',
                    duration: 2200,
                });
            },
        });
    });
}
